﻿using System;
using System.ComponentModel;
using System.Collections;

namespace sonic.Samples.Infrastructure.Model
{

    public class DomainObject : INotifyPropertyChanged
    {
        private int _id = -1;
        private string _DisplayName = string.Empty;
        private bool _IsDeleted = false;
        protected readonly Guid instanceId = Guid.NewGuid();

        public DomainObject() {
        }

        public virtual int Id {
            get { return _id; }
            set {
                _id = value;
                this.NotifyPropertyChanged("Id");
            }
        }

        public virtual string DisplayName {
            get { return _DisplayName; }
            set {
                _DisplayName = value;
                this.NotifyPropertyChanged("DisplayName");
            }
        }

        public virtual bool IsDeleted {
            get { return _IsDeleted; }
            set {
                _IsDeleted = value;
                this.NotifyPropertyChanged("IsDeleted");
            }
        }

        public override bool Equals(object obj) {
            if (this.Id < 0) {
                return (obj as DomainObject) != null &&
                   this.GetType().Equals(obj.GetType()) &&
                   this.instanceId == (obj as DomainObject).instanceId;
            }
            return (obj as DomainObject) != null &&
                   this.GetType().Equals(obj.GetType()) &&
                   this.Id == (obj as DomainObject).Id;
        }

        public override int GetHashCode() {
            return base.GetHashCode();
        }

        #region INotifyPropertyChanged Members

        /// <summary>
        /// Raised when a property on this object has a new value.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Raises this object's PropertyChanged event.
        /// </summary>
        /// <param name="propertyName">The property that has a new value.</param>
        protected virtual void NotifyPropertyChanged(string propertyName) {

            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null) {
                var e = new PropertyChangedEventArgs(propertyName);
                handler(this, e);
            }
        }

        #endregion // INotifyPropertyChanged Members
    }
}
